//
//  SuperviseTaskDetailView.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/1/9.
//

import Foundation
import UIKit

class SuperviseTaskDetailView: UIScrollView {
    let basicBV = UIView(frame: CGRect(x: 0, y: 0, width: ScreenWidth - 20, height: 124)) // 基本情况
    let bTitleL = UILabel()
    let detailBtn = UIButton.createBlue("检查基本信息")
    let dateL = UILabel()

    let issueBV = STaskIssueView(withTitle: "问题详情") // 问题详情
    let checkBV = STaskConditionView(withTitle: "检查情况", valKey: "检查总数", subValKey: "合格总数") // 检查情况
    let adjustBV = STaskConditionView(withTitle: "调整情况", valKey: "调整总数") // 调整情况

    let commonBV = UIView.createBase() // 评论
    let commonV = UIView()
    let commonTV = TitleItemView(withTitle: "评论", hasIcon: false)
    let writeTV = BSTextView()
    let submitBtn = UIButton.createPrimaryLarge("发送评论")
  
    override init(frame: CGRect) {
        super.init(frame: frame)
        showsVerticalScrollIndicator = false
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func reload(withModal modal: SpRwbgxqModal?) {
        bTitleL.text = modal?.rwlxmc
        dateL.text = "提交时间: \(modal?.tjsj ?? "")"
        
        if let mbs = modal?.rwjlMbs {
            var v = ""
            for (index, item) in mbs.enumerated() {
                if item.rwmc != nil {
                    if index != 0 {
                        v.append("\n")
                    }
                    v.append(item.rwmc!)
                }
            }
            issueBV.val = v
        }
        
        checkBV.val = modal?.jczs ?? 0
        checkBV.subVal = modal?.hgzs ?? 0
        adjustBV.val = modal?.tzzs ?? 0
    }
    
    func reload(withCommons commons: [YjwxCommonModal?]) {
        commonTV.subTitle = "（\(commons.count)）"
        
        commonV.removeAllSubViews()

        var lastI: RepairCommonView?
        for (index, common) in commons.enumerated() {
            let itemV = RepairCommonView()
            itemV.updateUI(withCommon: common!)
            commonV.addSubview(itemV)
            itemV.snp.makeConstraints { make in
                make.left.right.equalToSuperview()
                make.top.equalTo(lastI == nil ? commonV.snp.top : lastI!.snp.bottom)
                if index == commons.count - 1 {
                    make.bottom.equalToSuperview()
                }
            }
            lastI = itemV
        }
    }
    
    // 基本信息
    func setupBasicView() {
        basicBV.layer.cornerRadius = 10
        basicBV.layer.masksToBounds = true
        basicBV.drawBG([UIColor.hex("#2988E2").cgColor, UIColor.hex("#1E63C2").cgColor])
        addSubview(basicBV)
        basicBV.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
            make.height.equalTo(124)
        }
        
        bTitleL.textColor = .white
        bTitleL.font = .systemFont(ofSize: 17)
        basicBV.addSubview(bTitleL)
        bTitleL.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(20)
            make.left.equalToSuperview().offset(20)
        }
        
        basicBV.addSubview(detailBtn)
        detailBtn.snp.makeConstraints { make in
            make.centerY.equalTo(bTitleL.snp.centerY)
            make.right.equalToSuperview().offset(-24)
            make.height.equalTo(32)
            make.width.equalTo(108)
        }
        
        let line = UIView()
        line.backgroundColor = .white.withAlphaComponent(0.12)
        basicBV.addSubview(line)
        line.snp.makeConstraints { make in
            make.top.equalTo(bTitleL.snp.bottom).offset(20)
            make.left.equalTo(bTitleL.snp.left)
            make.right.equalTo(detailBtn.snp.right)
            make.height.equalTo(0.5)
        }
        
        dateL.textColor = .white
        dateL.font = .systemFont(ofSize: 16)
        basicBV.addSubview(dateL)
        dateL.snp.makeConstraints { make in
            make.top.equalTo(line.snp.bottom).offset(20)
            make.left.equalTo(bTitleL.snp.left)
        }
    }
    
    // 问题详情
    func setupIssueView() {
        issueBV.arrowIV.isHidden = true
        addSubview(issueBV)
        issueBV.snp.makeConstraints { make in
            make.top.equalTo(basicBV.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
        }
    }
    
    // 检查情况
    func setupCheckView() {
        addSubview(checkBV)
        checkBV.snp.makeConstraints { make in
            make.top.equalTo(issueBV.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
        }
    }
    
    // 调整情况
    func setupAdjustView() {
        addSubview(adjustBV)
        adjustBV.snp.makeConstraints { make in
            make.top.equalTo(checkBV.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
        }
    }
    
    func setupCommonView() {
        addSubview(commonBV)
        commonBV.snp.makeConstraints { make in
            make.top.equalTo(adjustBV.snp.bottom).offset(10)
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
            make.bottom.equalToSuperview().offset(-40)
        }
        
        commonBV.addSubview(commonTV)
        commonTV.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(46)
        }
        
        commonBV.addSubview(commonV)
        commonV.snp.makeConstraints { make in
            make.top.equalTo(commonTV.snp.bottom)
            make.left.right.equalToSuperview()
        }
        
        writeTV.placeholder = "写评论"
        writeTV.backgroundColor = .hex("#F9F9F9")
        writeTV.layer.cornerRadius = 10
        writeTV.layer.masksToBounds = true
        writeTV.textContainerInset = UIEdgeInsets(top: 10, left: 8, bottom: 10, right: 10)
        commonBV.addSubview(writeTV)
        writeTV.snp.makeConstraints { make in
            make.top.equalTo(commonV.snp.bottom).offset(10)
            make.left.equalToSuperview().offset(12)
            make.right.equalToSuperview().offset(-12)
            make.height.equalTo(80)
        }
        
        commonBV.addSubview(submitBtn)
        submitBtn.snp.makeConstraints { make in
            make.top.equalTo(writeTV.snp.bottom).offset(12)
            make.left.equalToSuperview().offset(12)
            make.right.equalToSuperview().offset(-12)
            make.height.equalTo(50)
            make.bottom.equalToSuperview().offset(-15)
        }
    }
    
    func setupUI() {
        // 基本信息
        setupBasicView()
        
        // 问题详情
        setupIssueView()
        
        // 检查情况
        setupCheckView()
        
        // 调整情况
        setupAdjustView()
        
        // 评论
        setupCommonView()
    }
}

// 情况
class STaskIssueView: UIControl {
    let adjustTV = TitleItemView(hasIcon: false)
    let arrowIV = UIImageView(image: UIImage(systemName: "chevron.right"))
    let titleL = UILabel()
    
    let valL = UILabel()
    
    var val: String? {
        didSet {
            valL.text = (val != nil && val != "") ? val :  "\n暂无问题\n"
            valL.textAlignment = (val != nil && val != "") ? .left : .center
        }
    }
    
    init(withTitle title: String) {
        super.init(frame: .zero)
        adjustTV.title = title
        layer.cornerRadius = 10
        layer.masksToBounds = true
        backgroundColor = .white
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        addSubview(adjustTV)
        adjustTV.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(46)
        }
        
        arrowIV.tintColor = .hex("#A5A5A5")
        arrowIV.contentMode = .scaleAspectFit
        addSubview(arrowIV)
        arrowIV.snp.makeConstraints { (make) -> Void in
            make.centerY.equalTo(adjustTV.snp.centerY)
            make.right.equalTo(self.snp.right).offset(-15)
            make.width.equalTo(10)
        }
        
        let line = UIView()
        line.backgroundColor = .hex("#ECECEC")
        addSubview(line)
        line.snp.makeConstraints { make in
            make.top.equalTo(adjustTV.snp.bottom)
            make.left.equalToSuperview().offset(11)
            make.right.equalToSuperview().offset(-13)
            make.height.equalTo(0.5)
        }
        
        valL.text = "暂无问题"
        valL.textAlignment = .center
        valL.textColor = .hex("#D1D1D1")
        valL.font = UIFont.systemFont(ofSize: 14)
        valL.numberOfLines = 0
        addSubview(valL)
        valL.snp.makeConstraints { make in
            make.top.equalTo(line.snp.bottom).offset(20)
            make.left.equalToSuperview().offset(11)
            make.right.equalToSuperview().offset(-11)
            make.bottom.equalToSuperview().offset(-20)
        }
    }
}


// 情况
class STaskConditionView: UIControl {
    let adjustTV = TitleItemView(hasIcon: false)
    let titleL = UILabel()
    
    let valL = UILabel()
    let subValL = UILabel()

    var valKey: String = ""
    var subValKey: String = ""
    
    var val: Int64? {
        didSet {
            valL.text = "\(valKey): \(val ?? 0)"
        }
    }
    
    var subVal: Int64? {
        didSet {
            subValL.text = "\(subValKey): \(subVal ?? 0)"
        }
    }
    
    init(withTitle title: String, valKey: String, subValKey: String = "") {
        super.init(frame: .zero)
        adjustTV.title = title
        self.valKey = valKey
        self.subValKey = subValKey
        
        layer.cornerRadius = 10
        layer.masksToBounds = true
        backgroundColor = .white
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        adjustTV.isUserInteractionEnabled = false
        addSubview(adjustTV)
        adjustTV.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(46)
        }
        
        let arrowIV = UIImageView(image: UIImage(systemName: "chevron.right"))
        arrowIV.tintColor = .hex("#A5A5A5")
        arrowIV.contentMode = .scaleAspectFit
        addSubview(arrowIV)
        arrowIV.snp.makeConstraints { (make) -> Void in
            make.centerY.equalTo(adjustTV.snp.centerY)
            make.right.equalTo(self.snp.right).offset(-15)
            make.width.equalTo(10)
        }
        
        let line = UIView()
        line.backgroundColor = .hex("#ECECEC")
        addSubview(line)
        line.snp.makeConstraints { make in
            make.top.equalTo(adjustTV.snp.bottom)
            make.left.equalToSuperview().offset(11)
            make.right.equalToSuperview().offset(-13)
            make.height.equalTo(0.5)
        }
        
        valL.textColor = .hex("#666666")
        valL.font = UIFont.systemFont(ofSize: 14)
        addSubview(valL)
        valL.snp.makeConstraints { make in
            make.top.equalTo(line.snp.bottom).offset(18)
            make.left.equalTo(self.snp.left).offset(11)
            make.bottom.equalToSuperview().offset(-20)
            make.width.equalTo((ScreenWidth - 80) / 2.0)
        }
        
        subValL.textColor = .hex("#666666")
        subValL.font = UIFont.systemFont(ofSize: 14)
        addSubview(subValL)
        subValL.snp.makeConstraints { make in
            make.top.equalTo(valL.snp.top)
            make.left.equalTo(valL.snp.right)
            make.bottom.equalToSuperview().offset(-20)
        }
    }
}
